//定义关于counter的store
import {defineStore} from 'pinia'
import {Service} from "../requests/index.ts";
import {message} from "ant-design-vue";
import project from "./project.js";

/*defineStore 是需要传参数的，其中第一个参数是id，就是一个唯一的值，
简单点说就可以理解成是一个命名空间.
第二个参数就是一个对象，里面有三个模块需要处理，第一个是 state，
第二个是 getters，第三个是 actions。
*/
const useFiber = defineStore("fiber",{
    state:() => ({
        fiberList: [],//Fiber列表
        currentFiber: {},//当前Fiber名称
        currentNet: 'NETONE',//当前网络
    }),

    getters: {
        //基本使用
        fiberNameListSelect(state) {
            return state.fiberList.map((item) => {
                return {value: item.name, label: item.name}
            })
        },
    },

    actions: {
        //基本使用
        selectCurrentFiber(name) {
            return this.currentFiber.name = name
        },
        //获取Fiber列表
        async getFiberList(projectName, net = 'NETONE') {
            this.currentNet = net
            const res=await Service.queryLinkUsingGet(projectName,'FIBER', net)
            if (res.respCode === 200) {
                console.log("getFiberList",res)
                this.fiberList = res.data
            }else {
                message.error(res.message);
            }
            return res.data
        },
        //添加单个Fiber
        async addSingleFiber(projectName,fiber, net = null) {
            const currentNetValue = net || this.currentNet
            console.log("fiber",fiber)
            const res=await Service.uploadSingFiberLinkUsingPost(projectName,fiber, currentNetValue)
            // console.log("res",res)
            if (res.respCode === 200) {
                this.fiberList= res.data;
                return res.data
            }else {
                message.error(res.message);
            }
            // console.log("addSingleFiber",this.fiberList)
            return this.fiberList
        },
        //文件上传
        async uploadFiberFile(file,projectName, net = null) {
            const currentNetValue = net || this.currentNet
            const res=await Service.uploadLinkUsingPost(file,projectName,"FIBER", currentNetValue)
            console.log("uploadFiberFile",res)
            if (res.respCode === 200) {
                this.fiberList= res.data;
                message.success(file.name+"上传成功")
                return res.data
            }else {
                message.error(res.message);
            }
            // console.log("addSingleFiber",this.fiberList)
            return this.fiberList
        },
        //删除Fiber
        async delSingleFiber(projectName,name, net = null) {
            const currentNetValue = net || this.currentNet
            const res=await Service.deleteNetUsingDelete(projectName,name,'FIBER', currentNetValue)
            console.log("deleteFiberUsingDelete",res)
            if (res.respCode === 200) {
                this.fiberList = res.data
                return res.data
            }else {
                message.error(res.message);
            }
            return this.fiberList
        },

        //修改单个Fiber
        async updateSingleFiber(projectName,fiber, net = null) {
            const currentNetValue = net || this.currentNet
            const res=await Service.modifyFiberUsingPut(projectName,fiber, currentNetValue)
            console.log("updateSingleFiber",res)
            if (res.respCode === 200) {
                this.fiberList = res.data
                return res.data
            }else {
                message.error(res.message);
            }
            return this.fiberList
        }
    }
})

//暴露这个fiber模块
export default useFiber
